package com.maomao.pic;

import com.maomao.utils.StringUtil;

import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by admin on 2018/3/23.
 */
public class ImageT {
    private Font font = new Font("", Font.PLAIN, 20);// 添加字体的属性设置
    private Graphics2D g = null;
    private int fontsize = 0;
    private int x = 0;
    private int y = 0;

    /**
     * 修改图片,返回修改后的图片缓冲区
     */
    public BufferedImage modifyImage(BufferedImage img, String content, int x,
                                     int y) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            g = img.createGraphics();
            g.setBackground(Color.WHITE);
            g.setColor(Color.RED);
            if (this.font != null )
                g.setFont(this.font);
            // 验证输出位置的纵坐标和横坐标
            if (x >= h || y >= w) {
                this.x = h - this.fontsize + 2;
                this.y = w;
            } else {
                this.x = x;
                this.y = y;
            }
            int lengthcl=(w-this.x)/this.fontsize;
            List<String> ls =StringUtil.getStrList(content,lengthcl);//分割字符串
            for(int i=0;i<ls.size();i++){
                g.drawString(ls.get(i), this.x, this.y);
                this.y+=30;
            }
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return img;
    }

    public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {
        try {
            int w = b.getWidth();
            int h = b.getHeight();
            g = d.createGraphics();
            g.drawImage(b, 100, 10, w, h,null );
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return d;
    }

    public static void main(String[] args) {

        ImageT tt = new ImageT();
        String str = "一个初创却无可ssd限量的团队：我们有来自于fff五百强企业的技术we宅男，有来自于知名房企的品牌经理，有来自于广告行业的资深销售。我们为同一个目标凝聚，励志做一个互联网时代企业革新的引导者，好伙伴。 ";
        BufferedImage d = tt.loadImageLocal("F:\\useFile\\test.jpg");
        tt.setFont("楷体", 30);
        tt.writeImageLocal("F:\\useFile\\write.jpg",tt.modifyImage(d,str,410,40));
        System.out.println("success");
    }
    /**
     * 导入本地图片到缓冲区
     */
    public BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null ;
    }

    /**
     * 导入网络图片到缓冲区
     */
    public BufferedImage loadImageUrl(String imgName) {
        try {
            URL url = new URL(imgName);
            return ImageIO.read(url);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 生成新图片到本地
     */
    public void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    /**
     * 设置文字字体
     */
    public void setFont(String fontStyle, int fontSize) {
        this.fontsize = fontSize;
        this.font = new Font(fontStyle, Font.PLAIN, fontSize);
    }
}
